﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Frame.Lib.Common;
using Frame.Lib.Core.User;
using Frame.Lib.Utilities;
using Frame.Lib.Core.Register;
using Frame.Lib.Core.Company;
using Frame.Lib.Core.Cache;
using Frame.Lib.Enums;
/// <summary>
///PageBase 的摘要说明
/// </summary>
public class PageBase : System.Web.UI.Page
{
    public PageBase()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
        
       
    }

    /// <summary>
    /// 获取当前地址的域名，不包括www,如：betop.sh.cn
    /// </summary>
    /// <returns></returns>
    public string GetDomain()
    {
        string url = Request.Url.Host;
        url = url.Replace("www.", "");
        return url;
    }

    public void Alert(string msg)
    {
        JSUtil.Alert(msg);
    }

   
   
    protected void Page_Init(object sender, EventArgs e)
    {
        //每次获最新页面
        Response.Buffer = true;
        Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
        Response.Expires = 0;
        Response.CacheControl = "no-cache";       

        bool expire = false;
       
     
        RegisterInfo info = CacheManager.Instance.SysConfigContainer.RegisterInfo;
        expire = (info.ExpireDate < DateTime.Now);

        if (expire)
        {
            string strScript = "alert('expire');top.location.href = '" + GlobalVar.WebAppPath + "login.aspx';";
            JSUtil.ResponseJS(strScript);
            return;
        }
        else
        {
            string mac = NetworkAdapter.GetFirstMacAddr();

            bool invalidMac = !info.Mac.Contains(mac);

            if (invalidMac)
            {
                string strScript = "alert('invalid mac');top.location.href = '" + GlobalVar.WebAppPath + "login.aspx';";
                JSUtil.ResponseJS(strScript);
                return;
            }
        }

        CheckLogin();
    }

    public static void CheckLogin()
    {
        if (IsTimeout())
        {
            string strScript = "alert('访问超时！');top.location.href = '" + GlobalVar.WebAppPath + "login.aspx';";
            JSUtil.ResponseJS(strScript);
        }
    }

    public static bool IsTimeout()
    {
        bool isTimeout = false;
        if (AppSession.UserName == null)
        {
            if (AppSetting.MultiCompany)
            {
                if (AppCookie.CompanyLoginName == null)
                {
                    return true;
                }
                else
                {
                    SetCompanySession(AppCookie.CompanyLoginName);
                }
            }

            UserBO user = new UserBO();
            if (AppCookie.LoginName != null)
            {
                LoginResult result = user.CheckLogin(AppCookie.LoginName, StrUtil.DecryptDES(AppCookie.Password));
                if (result == LoginResult.Success)
                {
                    SetSession(AppCookie.LoginName);
                }
                else
                {
                    isTimeout = true;
                }
            }
            else
            {
                isTimeout = true;
            }

        }
        return isTimeout;
    }

    public static void SetSession(string strLoginName)
    {
        UserBO user = new UserBO();

        UserInfo info = user.GetInfoByLoginName(strLoginName);
        AppSession.UserID = info.ID;
        AppSession.UserName = info.Name;
        AppSession.LoginName = info.LoginName;
        AppSession.DeptID = info.DeptIDs;
        AppSession.DeptName = info.DeptName;

        AppCookie.LoginName = info.LoginName;
        AppCookie.Password = StrUtil.EncryptDES(info.Password);
    }


    public static void SetCompanySession(string companyLoginName)
    {
        CompanyBO company = new CompanyBO();
        CompanyInfo comInfo = company.GetInfoByLoginName(companyLoginName);
        SetCompanySession(comInfo);
    }

    public static void SetCompanySession(CompanyInfo comInfo)
    {      
      
        AppSession.CompanyID = comInfo.ID;
        AppSession.CompanyLoginName = comInfo.LoginName;

        AppCookie.CompanyLoginName = comInfo.LoginName;
    }

    public static void ClearSession()
    {
        AppSession.Clear();
        AppCookie.Clear();
    }

    protected string Encrypt(object src)
    {
        return StrUtil.EncryptUrl(src);
    }

    protected string Decrypt(object src)
    {
        return StrUtil.DecryptUrl(src);
    }

    protected void AllowPageScroll()
    {
        WebUtil.AllowPageScroll(this);
    }

    protected void ShowForm(IAttributeAccessor button,string title, string url, int width, int height)
    {
        JSUtil.ShowForm(button, title, null, url, width, height);
    }

    protected void IncludeJsFile(string key,JsFile file)
    {
        this.ClientScript.RegisterClientScriptInclude(key, GlobalVar.WebAppPath + "Common/js/" + file.ToString() + ".js");
    }
}
